package itesm.coler.cloudsql;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import itesm.coler.models.User;

public class UserDAO {

	private static PreparedStatement insertaUsuario,regresaUsuario;
	private Connection connection;
	
	public UserDAO(){
		
	}
	public Connection getConnection(){
		return connection;
	}
	public void setConnection(Connection connection){
		this.connection = connection;
	}
	
	public void insertUser(User user){
		try {
			insertaUsuario = connection.prepareStatement("insert into user(username,firstname,middlename,lastname,gender,active,online,userType,password) values(?,?,?,?,?,?,?,?,?);");
			insertaUsuario.setString(1, user.getUserName());
			insertaUsuario.setString(2, user.getFirstName());
			insertaUsuario.setString(3, user.getMiddleName());
			insertaUsuario.setString(4, user.getLastName());
			insertaUsuario.setString(5, user.getGender()+"");
			insertaUsuario.setInt(6, 1);
			insertaUsuario.setInt(7, 0);
			insertaUsuario.setString(8, user.getUserType());
			insertaUsuario.setString(9, user.getPass());
			insertaUsuario.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public User getUser(String username) throws SQLException{
		regresaUsuario = connection.prepareStatement("select * from user where username = '"+username+"';");
		ResultSet ble = regresaUsuario.executeQuery();
		ble.next();
		
		User holo = new User();
		holo.setUserName(ble.getString("username"));
		holo.setFirstName(ble.getString("firstname"));
		holo.setMiddleName(ble.getString("middlename"));
		holo.setLastName(ble.getString("lastname"));
		holo.setPass(ble.getString("password"));
		
		return holo;
	}
	
}
